package com.ruoyi.system.host;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.apiResult.ChannelSupervise;
import com.ruoyi.common.cache.CacheComponent;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.system.apiService.ZhtcService;
import com.ruoyi.system.apiService.impl.ZhtcServiceImpl;
import com.ruoyi.system.common.WebsocketMsg;
import com.ruoyi.system.domain.ZsyyParkEnter;
import com.ruoyi.system.domain.ZsyyParkEnterPay;
import com.ruoyi.system.domain.ZsyyParkLeave;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

@Component
public class ServerPark {
	@Autowired
	private CacheComponent cacheComponent;  //Redis
	@Autowired
	private ZhtcServiceImpl zhtcService;
	//    返回的消息
	public WebsocketMsg getReturnMsg(String uuid) {
		WebsocketMsg re = null;
		try {

			String data = null;
			for (int i = 0; i < 20; i++) {
				Thread.sleep(200);
				data = cacheComponent.getRaw(uuid);
				if (!CommonUtil.isEmpty(data)) {
					break;
				}
			}
			if (CommonUtil.isEmpty(data)) {
				if(re==null){
					re = new WebsocketMsg();
					re.setCode(11);
					re.setMsg("远程连接失败");
				}
				return re;
			} else {
				WebsocketMsg jsonObj = JSON.parseObject(data, WebsocketMsg.class);
//                logger.info("data = " + data);
				return jsonObj;
			}

		} catch (Exception e) {
			e.printStackTrace();
			if(re==null){
				re = new WebsocketMsg();
				re.setCode(1);
				re.setMsg("远程连接失败");
			}
			return re;
		}
	}


	//进场上报
	public WebsocketMsg enterPush(WebsocketMsg param){
		try{
			param.setMsgType(2);
			param.setQueryType(2);
			ZsyyParkEnter parkEnter = JSON.parseObject(JSON.toJSONString(param.getData()),ZsyyParkEnter.class);
			zhtcService.addParkEnter(parkEnter);
		}catch (Exception e){
			e.printStackTrace();
		}

		return param;
	}

	//离场上报
	public WebsocketMsg leavePush(WebsocketMsg param){
		try{
			param.setMsgType(2);
			param.setQueryType(2);
			ZsyyParkLeave parkLeave = JSON.parseObject(JSON.toJSONString(param.getData()),ZsyyParkLeave.class);
			zhtcService.addParkLeave(parkLeave);
		}catch (Exception e){
			e.printStackTrace();
		}

		return param;
	}
	//操作白名单
	public WebsocketMsg operationTicket(WebsocketMsg<ZsyyParkLeave> param){
		try{
//            action：update_or_add 增加或更新白名单  delete 删除白名单
			param.setUuid(UUID.randomUUID().toString());
			param.setCmd("operationTicket");
			param.setMsgType(2);
			param.setQueryType(3);
			param.setUuid(UUID.randomUUID().toString());
			ChannelSupervise.uriSend("/websocket/host/"+param.getHostCode(),JSON.toJSONString(param));
			WebsocketMsg re = this.getReturnMsg(param.getUuid());
			return re;
		}catch (Exception e){
			e.printStackTrace();
		}

		return param;
	}

	//开闸
	public WebsocketMsg opGate(WebsocketMsg<Integer> param){
		param.setCmd("opGate");
		param.setMsgType(2);
		param.setQueryType(3);
		param.setUuid(UUID.randomUUID().toString());
		ChannelSupervise.uriSend("/websocket/host/"+param.getHostCode(), JSON.toJSONString(param));
		WebsocketMsg re = this.getReturnMsg(param.getUuid());
		return re;
	}

	//查费
	public WebsocketMsg getParkFee(WebsocketMsg<Map> param){//carNo车牌号
		param.setCmd("getParkFee");
		param.setMsgType(2);
		param.setQueryType(3);
		param.setUuid(UUID.randomUUID().toString());
		ChannelSupervise.uriSend("/websocket/host/"+param.getHostCode(),JSON.toJSONString(param));
		WebsocketMsg re = this.getReturnMsg(param.getUuid());
		return re;
	}

	//缴费
	public WebsocketMsg payParkFee(WebsocketMsg<ZsyyParkEnterPay> param){//carNo车牌号
		param.setCmd("payParkFee");
		param.setMsgType(2);
		param.setQueryType(3);
		param.setUuid(UUID.randomUUID().toString());
		ChannelSupervise.uriSend("/websocket/host/"+param.getHostCode(),JSON.toJSONString(param));
		WebsocketMsg re = this.getReturnMsg(param.getUuid());
		return re;
	}


}
